package site.lanmushan.generate.entity;


import lombok.Data;
import site.lanmushan.generate.util.StringUtil;

/**
 * @author Administrator
 * @date 2017/5/16
 */
@Data
public class Column {
    private String table_name;
    private String column_name;
    //UNI 代表唯一索引
    private String column_key;
    private String extra;
    /**
     * 列注释
     */
    private String column_comment;
    private String data_type;
    private String is_nullable;
    private Long character_maximum_length;

    private String propertyName;

    private String javaType;
    private String jdbcType;
    private String tsType;
    private Integer unique = 0;

    public void init() {
        this.propertyName = StringUtil.underlineToCamel(column_name, '_');
        this.createPropertyType();
        if ("UNI".equals(column_key)) {
            unique = 1;
        }
    }

    private void createPropertyType() {
        switch (data_type) {
            case "varchar":
            case "text":
            case "longtext": {
                this.javaType = "String";
                this.tsType = "string";
                this.jdbcType="VARCHAR";
                break;
            }
            case "tinyint":
            case "int": {
                this.javaType = "Integer";
                this.tsType = "number";
                this.jdbcType="INTEGER";

                break;
            }
            case "datetime": {
                this.javaType = "LocalDateTime";
                this.tsType = "string";
                this.jdbcType="TIMESTAMP";

                break;
            }
            case "date": {
                this.javaType = "LocalDate";
                this.tsType = "string";
                this.jdbcType="TIMESTAMP";

                break;
            }
            case "decimal": {
                this.javaType = "BigDecimal";
                this.tsType = "string";
                this.jdbcType="DECIMAL";

                break;
            }
            case "float": {
                this.javaType = "Float";
                this.tsType = "number";
                this.jdbcType="FLOAT";
                break;
            }
            case "double": {
                this.javaType = "Double";
                this.tsType = "string";
                this.jdbcType="DOUBLE";

                break;
            }
            case "bigint": {
                this.javaType = "Long";
                this.tsType = "string";
                this.jdbcType="BIGINT";
                break;
            }
            case "json": {
                this.javaType = "JSONObject";
                this.tsType = "any";
                this.jdbcType="VARCHAR";
                break;
            }
            default: {
                this.javaType = "String";
                this.tsType = "string";
                this.jdbcType="VARCHAR";

            }
        }
    }
}
